<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // 创建ws实例，建立连接  (ws://121.40.165.18:8800  有广告)
    var ws = new WebSocket("wss://javascript.info/article/websocket/demo/hello");

    // 连接成功事件
    ws.onopen = function (evt) {
      console.log("Connection open ...");
      // 发送消息
      ws.send("Hello WebSockets!");
    };
    // 接受消息事件
    ws.onmessage = function (evt) {
      console.log("Received Message: " + evt.data);
      // 关闭连接
      ws.close();
    };
    // 关闭连接事件
    ws.onclose = function (evt) {
      console.log("Connection closed.");
    };

  </script>
</body>

</html>